 

import Control.*;
import Model.*;

/**
 * UI for products
 * @author Stefan
 */
public class ProductsUI {

    private ProductController proCtr = ProductController.getInstance();

    /**
     * Start product menu
     */
    public void start()
    {
        productsMenu();
    }

    /**
     * Product menu
     */
    public void productsMenu()
    {
        boolean exit = false;
        while (!exit)
        {
            int choise = writeProductMenu();
            if (choise == 1)
            {
                startCreate(CommonUIFunctions.readString("\n write ID: "), CommonUIFunctions.readString("\n write product name: "), CommonUIFunctions.readString("\n write description: "), CommonUIFunctions.readDouble("\n write price: "), CommonUIFunctions.readDouble("\n write rental price: "), 0, CommonUIFunctions.readInt("\n set minimum stock: "));
            }
            else if (choise == 2)
            {
                startRead(CommonUIFunctions.readString("\n write ID of the product you want to find: "));
            }
            else if (choise == 3)
            {
                startUpdate(CommonUIFunctions.readString("\n write the ID of the product you want to update: "));
            }
            else if (choise == 4)
            {
                startDelete(CommonUIFunctions.readString("\n write the ID of the product you want to delete: "));
            }
            else
            {
                exit = true;
            }
        }
    }

    /**
     * Print Product menu
     * @return int based on your choise
     */
    private int writeProductMenu()
    {
        System.out.println("\n\f *** Productmenu ***");
        System.out.println(" (1) Create");
        System.out.println(" (2) Read");
        System.out.println(" (3) Update");
        System.out.println(" (4) Delete");
        System.out.println(" (0) Back");

        return CommonUIFunctions.readInt("\n Make your choise: ");
    }

    /**
     * Create menu
     * @param id
     * @param name
     * @param description
     * @param price
     * @param rentalPrice
     * @param sale
     * @param minStock
     */
    private void startCreate(String id, String name, String description, double price, double rentalPrice, double sale, int minStock)
    {
        //If false write error message
        if (proCtr.addProduct(id, name, description, price, rentalPrice, sale, minStock) == false)
        {
            System.out.println("Product ID is already in use, choose another");
        }
    }

    /**
     * Get info about Product
     * @param ID
     * @return Product
     */
    private Product getInfo(String ID)
    {
        Product pro = proCtr.findProduct(ID);
        System.out.println("Product");
        System.out.println("Id: " + pro.getId());
        System.out.println("Name: " + pro.getName());
        System.out.println("Description: " + pro.getDescription());
        System.out.println("Price: " + pro.getPrice());
        if (pro.getRentalPrice() > 0)
        {
            System.out.println("Rental price: " + pro.getRentalPrice());
        }
        if (pro.getSale() <= 0)
        {
            System.out.println("Sale: not on sale");
        }
        else
        {
            System.out.println("Sale: " + pro.getSale() + " %");
            System.out.println("Sales price: " + pro.Price());
        }
        System.out.println("Total quantity: " + pro.copies());
        if (pro.stockStatus() == false)
        {
            System.out.println("Warning: the stock is running low. Reorder as soon as posible");
        }
        if (pro.copies() > 0)
        {
            listLocations(pro);
        }
        return pro;
    }

    /**
     * Print if no product with ID
     * @param ID
     */
    private void noProduct(String ID)
    {
        System.out.println("There was no product with this ID: " + (ID));
    }

    /**
     * Read menu
     * @param ID
     */
    private void startRead(String ID)
    {
        try
        {

            getInfo(ID);
        }
        catch (Exception e)
        {
            noProduct(ID);
        }
    }

    /**
     * Delete menu
     * @param ID
     */
    private void startDelete(String ID)
    {
        try
        {

            getInfo(ID);
            proCtr.deleteProduct(ID);
            System.out.println("The product was deleted");
        }
        catch (Exception e)
        {
            noProduct(ID);
        }

    }

    /**
     * Start Update menu
     * @param ID
     */
    private void startUpdate(String ID)
    {
        try
        {
            updateMenu(getInfo(ID));
        }
        catch (Exception e)
        {
            noProduct(ID);
        }
    }

    /**
     * Update menu
     * @param pro
     */
    private void updateMenu(Product pro)
    {
        boolean exit = false;
        while (!exit)
        {
            int choise = writeUpdateMenu();
            if (choise == 1)
            {
                String newID = CommonUIFunctions.readString("\n write new ID: ");
                while (proCtr.checkExistingID(newID))
                {
                    newID = CommonUIFunctions.readString("\n Product ID is already in use, choose another: ");
                }
                pro.setId(newID);
            }
            else if (choise == 2)
            {
                pro.setName(CommonUIFunctions.readString("\n write new name: "));
            }
            else if (choise == 3)
            {
                pro.setDescription(CommonUIFunctions.readString("\n write new description: "));
            }
            else if (choise == 4)
            {
                pro.setPriceLimit(CommonUIFunctions.readDouble("\n write new price: "));
            }
            else if (choise == 5)
            {
                pro.setRentalPriceLimit(CommonUIFunctions.readDouble("\n write new rental price: "));
            }
            else if (choise == 6)
            {
                pro.setSaleLimit(CommonUIFunctions.readDouble("\n write new sales percent (0-100): "));
            }
            else if (choise == 7)
            {
                listLocations(pro);
                int loc = (CommonUIFunctions.readInt("\n Enter location of the new goods: "));
                if (locationTest(loc) == false)
                {
                    noPlace();
                }
                else
                {
                    pro.addGoods(CommonUIFunctions.readInt("\n Enter quantity of the new goods: "), loc);
                }
            }
            else if (choise == 8)
            {
                listLocations(pro);
                int oldLoc = (CommonUIFunctions.readInt("\n Enter the location where the goods are moved from: "));
                if (locationTest(oldLoc) == false)
                {
                    noPlace();
                }
                else
                {
                    int newLoc = (CommonUIFunctions.readInt("\n Enter the location where the goods are moved to: "));
                    if (locationTest(newLoc) == false)
                    {
                        noPlace();
                    }
                    else
                    {
                        int quan = (CommonUIFunctions.readInt("\n Enter quantity: "));
                        pro.moveItem(quan, oldLoc, newLoc);
                    }
                }
            }
            else if (choise == 9)
            {
                pro.setMinStockLimit(CommonUIFunctions.readInt("\n set minimum stock: "));
            }
            else
            {
                exit = true;
            }
        }
    }

    /**
     * Print locations
     * @param pro
     */
    public void listLocations(Product pro)
    {
        System.out.println("\n 1 = " + getLocation1() + " : " + pro.locationQuantity(1));

        System.out.println(" 2 = " + getLocation2() + " : " + pro.locationQuantity(2));

        System.out.println(" 3 = " + getLocation3() + " : " + pro.locationQuantity(3));
    }

    /**
     * Location test
     * @param loc
     * @return true if succesful
     */
    private boolean locationTest(int loc)
    {
        boolean test = false;
        if (loc >= 1 || loc <= 3)
        {
            test = true;
        }
        return test;
    }

    /**
     * Print if location doesn't exists
     */
    private void noPlace()
    {
        System.out.println("\n Location doesn't exists");
    }

    public String getLocation1()
    {
        return "Tømmerhandel ";
    }

    public String getLocation2()
    {
        return "Byggemarked ";
    }

    public String getLocation3()
    {
        return "Leverandør ";
    }

    /**
     * Print update menu
     * @return int based on your choise
     */
    private int writeUpdateMenu()
    {
        System.out.println("\n\f *** UpdateMenu ***");
        System.out.println("\f Choose one to edit");
        System.out.println(" (1) Id ");
        System.out.println(" (2) Product name");
        System.out.println(" (3) Description");
        System.out.println(" (4) Price");
        System.out.println(" (5) Rental price");
        System.out.println(" (6) Sale");
        System.out.println(" (7) Add/remove goods");
        System.out.println(" (8) Change location");
        System.out.println(" (9) Set minimum stock");
        System.out.println(" (0) Back");

        return CommonUIFunctions.readInt("\n Make your choise: ");
    }
}
